﻿using PCI1616DLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using OCR.Function;

namespace OCR.SystemThread
{
    public class IOThread_Run
    {
        public static bool DIORun = false;
        public static Thread DIO_StateThread = null;
        public static int[] Do = new int[16];
        public static int[] Di = new int[16];
        public static void DIO_State()
        {
            DIORun = true;
            DIO_StateThread = new Thread(DIOThread);
            DIO_StateThread.IsBackground = true;
            DIO_StateThread.Start();
        }
        /// <summary>
        /// DIO線程掃描，一直持續更新狀態
        /// </summary>
        /// <param name="obj"></param>
        private static void DIOThread(object obj)
        {
            while (DIORun)
            {
                DI_State_Results();
                //DO_Trigger();
                Thread.Sleep(1);
            }
        }
        public static void DO_Trigger_olng(UInt16 Num,int nState)
        {
           int doState= XCDotNETAPI_PCI1616.DO_SetBit(PCI1616_Initial.hDevice, Num, nState, Do);
            Console.WriteLine("doState:-------------"+ doState);
        }
        /// <summary>
        /// DO輸出狀態更新
        /// </summary>
        /// <param name="sNodeID"></param>
        public static void DO_Trigger()
        {
            for (UInt16 i = 0; i < 16; i++)
            {
                if (Do[i] == 1)
                {
                    //LTDMC.dmc_write_outbit(sNodeID, i, 0);     //需要輸出時，下達0為輸出作動
                    XCDotNETAPI_PCI1616.DO_SetBit(PCI1616_Initial.hDevice, i, 1, Do);
                }
                else
                {
                    //LTDMC.dmc_write_outbit(sNodeID, i, 1);      //下達1為關閉輸出
                    XCDotNETAPI_PCI1616.DO_SetBit(PCI1616_Initial.hDevice, i, 0, Do);
                }
            }
        }
        /// <summary>
        /// DI輸入狀態更新
        /// </summary>
        /// <param name="sCount">控制卡編號</param>
        /// <param name="sNodeID">暫時不用</param>
        /// <returns></returns>
        public static short DI_State_Results()
        {
            short err = 0;
            //uint unN = LTDMC.dmc_read_inport(sCount, 0);//读本地输入口的值
            XCDotNETAPI_PCI1616.DI(PCI1616_Initial.hDevice, 0, 1, Di);
            for (int i = 0; i < 16; i++)
            {
                //if ((unN & 1) == 0)  //當判斷為0時才是有訊號進來
                //{
                //    Di[i] = 1;
                //}
                //else Di[i] = 0;
                //unN = unN >> 1;//unN变成unN向右移一位的那个数
            }
            return err;
        }

    }
}
